package cz.mff.cuni.dpp.liboptions;

import org.junit.Test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cz.mff.cuni.dpp.liboptions.OptionCallback;
import cz.mff.cuni.dpp.liboptions.Options;
import cz.mff.cuni.dpp.liboptions.Result;
import cz.mff.cuni.dpp.liboptions.StringOption;
import cz.mff.cuni.dpp.liboptions.SwitchOption;

public class SimpleTest {
	private static final Logger logger = LoggerFactory.getLogger(TimeTest.class);

	public static void main(String[] args) {

		Options options = new Options();

		// vytvori novy callback pro volbu typu word
		OptionCallback<String> wordCallback = new OptionCallbackAdapter<String>() {
			public void handleOptionValue(String parameter) {
				logger.info("word argument has parameter: {}", parameter);
			}
		};

		// vytvori novou volbu --word s retezcovym parametrem
		// WORD funguje jako nazev parametru, bude videt v helpu: --word=WORD
		// (resp. -w WORD)
		StringOption wordOption = new StringOption.Builder("word", "WORD").addAlias("w")
				// prida kratkou volbu -s jako synonymum
				.setDescription("testovaci option typu string").setCallback(wordCallback)
				.setObligatory(true) // nastavi volbu jako povinnou
				.buildOption();

		// prida volbu do Options
		options.addOptionDefinition(wordOption);

		OptionCallback<Integer> numberCallback = new OptionCallbackAdapter<Integer>() {
			public void handleOptionValue(Integer parameter) {
				logger.info("number argument has parameter: {}", parameter);
			}
		};

		// --number=NUMBER, -n NUMBER
		IntegerOption numberOption = new IntegerOption.Builder("number", "NUMBER").addAlias("n")
				// prida kratkou volbu -i jako synonymum
				.setDescription("testovaci option typu integer").setCallback(numberCallback)
				.buildOption();

		options.addOptionDefinition(numberOption);

		// vytvori callback pro booleovskou volbu (switch)
		OptionCallback<Boolean> switchCallback = new OptionCallback<Boolean>() {
			public void handleOptionValue(Boolean parameter) {
				logger.info("switch is on");
			}

			public void handleNotPresent() {
				logger.info("switch is off");
			}
		};

		SwitchOption switchOption = new SwitchOption.Builder("switch").addAlias("s")
				// prida kratkou volbu -i jako synonymum
				.setDescription("testovaci option typu integer").setCallback(switchCallback)
				.buildOption();

		options.addOptionDefinition(switchOption);

		OptionCallback<String> enumCallback = new OptionCallbackAdapter<String>() {
			public void handleOptionValue(String parameter) {
				logger.info("enum argument has parameter: {}", parameter);
			}
		};

		EnumOption enumOption = new EnumOption.Builder("enum", "WORD-FROM-SET").addAlias("e")
				.setDescription("vyber slov z mnoziny: alpha, beta, gama")
				.addAllowedValues("alpha", "beta", "gama").setCaseSensitive(false) // vypne
																					// rozlisovani
																					// velkych
																					// malych
																					// pismen
				.setCallback(enumCallback).buildOption();

		options.addOptionDefinition(enumOption);

		Result result = options.process(args);

		if (result.hasErrors()) {
			options.printHelp();
		} else {
			result.callCallbacks(true);
		}

		options.printHelp("./test [options] [argumennts]");
	}

	@Test
	public void sipleTest() {
		SimpleTest.main(new String[] { "--number=123", "--word=abcdefgh", "--enum=aLpHa",
				"--switch" });
	}
}
